<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
<form action="" method="post">
	<h1>添加学生信息</h1>
	N&nbsp;&nbsp;o:&nbsp;<input type="text" name="studentno" required="required" id="checkun">
	<a href="?action=checkregister&studentno=" onclick="this.href=this.href+document.getElementById('checkun').value">用户名是否存在?</a><br/><br/>
<?php
if(isset($_GET["action"])&&$_GET["action"]=="checkregister"){
	//此次事件是点击 用户名是否存在链接触发的事件
	$studentno=$_GET["studentno"];
	//操作数据库
	//下面开始判断是否存在同名的用户
	$checkunsql="select * from students where studentno='$studentno'";
	include_once("database.php");
	$result=@mysql_query($checkunsql) or die("查询同序号的时候出现错误!");
	$result=mysql_num_rows($result);
	if($result>0){
		//存在同名记录
		echo "<font color=\"red\">序号{$studentno}已经被添加,请重新填写序号!</font><br/><br/>";
	}else{
		//不存在同名记录
		echo "<font color=\"green\">序号{$studentno}没有被添加,可以使用!</font><br/><br/>";
	}
}
?>
	姓名:&nbsp;<input type="text" name="studentname"><br/><br/>
	性别:&nbsp;<input type="radio" name="xingbie" value="男" checked="checked">男&nbsp;&nbsp; 
	<input type="radio" name="xingbie" value="女">女<br/><br/>
	学号:&nbsp;<input type="text" name="number"><br/><br/>
	班级:&nbsp;<input type="text" name="class"><br/><br/>
	手机号:&nbsp;<input type="text" name="mobile"><br/><br/>
	信用:&nbsp;<input type="radio" name="credit" value="差" checked="checked">差 
	&nbsp;<input type="radio" name="credit" value="合格">合格
	&nbsp;<input type="radio" name="credit" value="良好">良好
	&nbsp;<input type="radio" name="credit" value="优秀">优秀
	<br/><br/>
	备注信息: <br/> <textarea name="studentinfos" rows="10" 
	cols="140"></textarea><br/><br/>
	<input type="reset" name="reset" value="重置">&nbsp;&nbsp;
	<input type="submit" name="submit" value="添加">
</form>
<?php 

if(isset($_POST["submit"])&&$_POST["submit"]=="添加"){
	include_once("database.php");
	$studentno=$_POST['studentno'];
	$studentname=$_POST['studentname'];
	$xingbie=$_POST['xingbie'];
	$number=$_POST['number'];
	$class=$_POST['class'];
	$mobile=$_POST['mobile'];
	$credit=$_POST['credit'];
	$studentinfos=$_POST['studentinfos'];
	
	if($studentno !=""&&$studentname !=""&&$xingbie !=""&&$number !=""&&$class !=""&&$mobile !=""&&$credit !=""&&$studentinfos !=""){
	$checkunsql="select * from students where studentno='$studentno'";
	$result=@mysql_query($checkunsql) or die("查询同序号的时候出现错误!".mysql_error()."<br/>");
	if(mysql_num_rows($result)>0){
		//存在同名记录
		echo "<script>alert('序号{$studentno}已经被添加,请重新填写序号!');</script>";
	}else{
		
	$insertstudents="insert into students(studentno,studentname,
		xingbie,number,class,mobile,credit,studentinfos) values(
			'$studentno','$studentname','$xingbie','$number','$class','$mobile','$credit','$studentinfos')";
	@mysql_query($insertstudents) or die("插入新学生失败:".mysql_error()."<br/>");
	echo "<script>alert('添加学生成功!');</script>";
}
}else{
	echo "<p style=\"color:red;\">学生信息不能为空!</p>";
}
}
?>
</body>
</html>